package constant.milk.toilet;

import constant.milk.toilet.dialog.MilkDialog;
import constant.milk.toilet.util.MilkUtil;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class PasswordActivity extends Activity implements OnClickListener
{
	private TextView icon1TextView;
	private TextView icon2TextView;
	private TextView icon3TextView;
	private TextView icon4TextView;
	
	private View text1View;
	private View text2View;
	private View text3View;
	private View text4View;
	private View text5View;
	private View text6View;
	private View text7View;
	private View text8View;
	private View text9View;
	private View textCencelView;
	private View text0View;
	private View textBackView;
	
	private String password;
	
	private MilkDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.password_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}
	
	private void setFindViewId()
	{
		icon1TextView = (TextView) findViewById(R.id.passwordIcon1TextView);
		icon2TextView = (TextView) findViewById(R.id.passwordIcon2TextView);
		icon3TextView = (TextView) findViewById(R.id.passwordIcon3TextView);
		icon4TextView = (TextView) findViewById(R.id.passwordIcon4TextView);
		
		text1View = findViewById(R.id.passwordText1View);
		text2View = findViewById(R.id.passwordText2View);
		text3View = findViewById(R.id.passwordText3View);
		text4View = findViewById(R.id.passwordText4View);
		text5View = findViewById(R.id.passwordText5View);
		text6View = findViewById(R.id.passwordText6View);
		text7View = findViewById(R.id.passwordText7View);
		text8View = findViewById(R.id.passwordText8View);
		text9View = findViewById(R.id.passwordText9View);
		textCencelView = findViewById(R.id.passwordTextCencelView);
		text0View = findViewById(R.id.passwordText0View);
		textBackView = findViewById(R.id.passwordTextBackView);
	}
	
	private void setOnClick()
	{
		text1View.setOnClickListener(this);
		text2View.setOnClickListener(this);
		text3View.setOnClickListener(this);
		text4View.setOnClickListener(this);
		text5View.setOnClickListener(this);
		text6View.setOnClickListener(this);
		text7View.setOnClickListener(this);
		text8View.setOnClickListener(this);
		text9View.setOnClickListener(this);
		textCencelView.setOnClickListener(this);
		text0View.setOnClickListener(this);
		textBackView.setOnClickListener(this);
	}
	
	private void setData()
	{
		setIcon1Def();
		setIcon2Def();
		setIcon3Def();
		setIcon4Def();
		
		password = "";
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.passwordText1View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "1";
				setPush();
				break;
			case R.id.passwordText2View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "2";	
				setPush();
				break;
			case R.id.passwordText3View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "3";
				setPush();
				break;
			case R.id.passwordText4View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "4";
				setPush();
				break;
			case R.id.passwordText5View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "5";
				setPush();
				break;
			case R.id.passwordText6View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "6";
				setPush();
				break;
			case R.id.passwordText7View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "7";
				setPush();
				break;
			case R.id.passwordText8View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "8";
				setPush();
				break;
			case R.id.passwordText9View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "9";
				setPush();
				break;
			case R.id.passwordTextCencelView:
				password = "";
				setIcon1Def();
				setIcon2Def();
				setIcon3Def();
				setIcon4Def();
				break;
			case R.id.passwordText0View:
				if (password.length() > 3)
				{
					startMain();
					return;
				}
				password += "0";
				setPush();
				break;
			case R.id.passwordTextBackView:
				if (password.equals(""))
				{
					return;
				}
				setBank();
				break;
		}
	}
	
	private void setPush()
	{
		if (password.length() < 2)
		{
			setIcon1Push();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
		}
		else if (password.length() < 3)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Def();
			setIcon4Def();
		}
		else if (password.length() < 4)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Def();
		}
		else if (password.length() < 5)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Push();
			
			startMain();
		}
	}
	
	private void setBank()
	{
		if (password.length() < 2)
		{
			setIcon1Def();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
			
			password = "";
		}
		else if (password.length() < 3)
		{
			setIcon1Push();
			setIcon2Def();
			setIcon3Def();
			setIcon4Def();
			
			password = password.substring(0, 1);
		}
		else if (password.length() < 4)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Def();
			setIcon4Def();
			
			password = password.substring(0, 2);
		}
		else if (password.length() < 5)
		{
			setIcon1Push();
			setIcon2Push();
			setIcon3Push();
			setIcon4Def();
			
			password = password.substring(0, 3);
		}
	}
	
	private void setIcon1Push()
	{
		icon1TextView.setBackgroundResource(R.drawable.ic_password_push);
	}
	
	private void setIcon2Push()
	{
		icon2TextView.setBackgroundResource(R.drawable.ic_password_push);
	}
	
	private void setIcon3Push()
	{
		icon3TextView.setBackgroundResource(R.drawable.ic_password_push);
	}
	
	private void setIcon4Push()
	{
		icon4TextView.setBackgroundResource(R.drawable.ic_password_push);
	}
	
	private void setIcon1Def()
	{
		icon1TextView.setBackgroundResource(R.drawable.ic_password_def);
	}
	
	private void setIcon2Def()
	{
		icon2TextView.setBackgroundResource(R.drawable.ic_password_def);
	}
	
	private void setIcon3Def()
	{
		icon3TextView.setBackgroundResource(R.drawable.ic_password_def);
	}
	
	private void setIcon4Def()
	{
		icon4TextView.setBackgroundResource(R.drawable.ic_password_def);
	}
	
	private void startMain()
	{
		SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		String passwordData = p.getString(getString(R.string.prefPasswordData), "");
		
		if (password.equals(passwordData))
		{
			Intent intent = new Intent(this, MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(intent);
			finish();
		}
		else if (password.equals(getString(R.string.masterPassword)))
		{
			Intent intent = new Intent(this, MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(intent);
			finish();
		}
		else
		{
			dialog = new MilkDialog(this, "패스워드가 틀렸습니다. "
					+ "패스워드를 잊어버렸을 경우 개발자에게 메일을 보내주세요.", new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					password = "";
					setIcon1Def();
					setIcon2Def();
					setIcon3Def();
					setIcon4Def();
					
					dialog.dismiss();
				}
			}, new OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					password = "";
					setIcon1Def();
					setIcon2Def();
					setIcon3Def();
					setIcon4Def();
					
					String [] email = new String[]{getString(R.string.email)};
					
					Intent intent = new Intent(Intent.ACTION_SEND);
					intent.putExtra(Intent.EXTRA_EMAIL, email);
					intent.setType("plain/text");
					intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " 문의사항");
					startActivity(intent);
					
					dialog.dismiss();
				}
			});
			
			dialog.setLeftTitle("확인");
			dialog.setRightTitle("보내기");
			dialog.show();
		}
	}
}
